<template>
  <div class="tabs-wrapper v-shadow v-radius">
    <nav class="tabs">
      <a
        v-for="(tab, i) of tabs"
        :key="i"
        href="#"
        class="tab v-radius"
        :class="{ 'tab-active': isShow(i) }"
        :ref="(el) => receiveTab(el, i)"
        v-ripple
        @click="tabCut(i, tab)"
      >{{ tab }}</a>
      <div class="animation start-home"></div>
    </nav>
    <div class="tabs-content">
      <!--mode="out-in" tag="div" appear-->
      <template v-for="(tab, i) of tabs">
        <div v-if="isShow(i)" :key="i">
          <slot :name="i" class="v-p" />
        </div>
      </template>
      <!-- <transition-group name="v-tabs" mode="out-in" appear>

      </transition-group>-->
    </div>
  </div>
</template>

<script lang="ts">
import { ref, onMounted, nextTick, computed, defineComponent } from "vue";
import { primaryColor } from '@/config/layout.ts';

export default defineComponent({
  emits: ['change'],
  props: {
    tabs: {
      type: Array,
      default: () => ["标签1", "标签2"],
    },
    bg: {
      type: String,
      default: "#fff",
    },
    color: {
      type: String,
      default: "#000",
    },
    animationColor: {
      type: String,
      default: primaryColor,
    },
    width: {
      type: String,
      default: "",
    }
  },
  setup(props, ctx) {
    const emit = ctx.emit;
    const tabsInfo = ref<
      Array<{ $el: HTMLElement; length: number; id: number }>
    >([]);
    const animationWidth = ref("0px");
    const animationLeft = ref("0px");
    const tabsWidth = ref("0px");
    const is = ref(0);
    const docState = ref('saved');

    /* 跳转标签 */
    const tabCut = (index: number, tab: any) => {
      const current = tabsInfo.value.find((info) => info.id === index);
      const frontTabsInfo = tabsInfo.value.filter((info) => info.id < index);
      const left = frontTabsInfo.reduce((left, info) => (left += info.length), 0);
      animationLeft.value = `${left}px`;
      animationWidth.value = `${current?.length}px`;
      is.value = index;
      emit("change", { ...current, name: tab });
    };
    /* 接收元素 */
    const receiveTab = (el: any, index: number) => {
      if (tabsInfo.value.findIndex(info => info.id === index) === -1) {
        const length = el.innerText.length * 40;
        el.style.width = `${length}px`;
        el.dataset.id = `${index}`;
        tabsInfo.value.push({ $el: el, length, id: index });
        if (index === 0) {
          animationWidth.value = `${length}px`;
        }
      }
    };

    /* 是否显示当前组件 */
    const isShow = computed(() => (i: number) => i === is.value);

    onMounted(() => {
      nextTick(() => {
        const width = tabsInfo.value.reduce((width, info) => (width += info.length), 0);
        tabsWidth.value = props.width || `${width}px`;
      });
    });

    return {
      isShow,
      receiveTab,
      tabCut,
      docState,
      is,
      tabsWidth,
      animationLeft,
      animationWidth,
      tabsInfo
    }
  }
})

</script>

<style scoped>
.v-tabs-enter-active {
  transition: all 0.25s ease-out;
}

.v-tabs-leave-active {
  transition: all 0.25s cubic-bezier(1, 0.5, 0.8, 1);
}

.v-tabs-enter-from,
.v-tabs-leave-to {
  transform: translateX(20px);
  opacity: 0;
}

.tabs-wrapper {
  width: v-bind(tabsWidth);
  overflow: hidden;
}
.tabs {
  position: relative;
  height: 50px;
  font-size: 0;
  background-color: v-bind(bg);
  width: v-bind(tabsWidth);
}

.tab {
  font-size: 15px;
  text-transform: uppercase;
  color: v-bind(color);
  text-decoration: none;
  line-height: 50px;
  display: inline-block;
  text-align: center;
  transition: all 0.25s ease-out;
}

.tab:hover {
  background-color: #e5e5e4;
}

.tab-active {
  background-color: #e5e5e4;
}

.tabs .animation {
  position: absolute;
  height: 5%;
  bottom: 0;
  background-color: v-bind(animationColor);
  border-radius: 8px;
  -webkit-border-radius: 8px;
  -moz-border-radius: 8px;
  -ms-border-radius: 8px;
  -o-border-radius: 8px;
  transition: all 0.5s ease 0s;
  -webkit-transition: all 0.5s ease 0s;
  -moz-transition: all 0.5s ease 0s;
  -ms-transition: all 0.5s ease 0s;
  -o-transition: all 0.5s ease 0s;
  width: v-bind(animationWidth);
  left: v-bind(animationLeft);
}
.tabs-content {
  width: v-bind(tabsWidth);
  background-color: #fff;
  color: #000;
}
</style>